const express = require("express")
//创建路由对象
const order = express.Router()
//引入数据库
const pool = require('../pool/pool');
// 引入表单验证部分
const Joi = require('joi');
// 引入封装后的响应
const Response = require('../utils/Response');

//1.查询订单接口

order.post('/select', (req, res, next) => {
  let obj = req.body;
  let uid = obj.uid;
  let status = obj.status;
  let pageNum = obj.pageNum;
  let pageSize = obj.pageSize;
  // 1.1啥都没传   就查询所有的订单,给后台管理员用************
  if (!uid && !status) {
    const sql = `select * from pet_po`;
    pool.query(sql, (err, result) => {
      if (err) {
        next(err)
        return
      }
      // console.log(result);
      if (result.length === 0) {
        res.send({ code: 201, msg: "没有订单" })
      } else {
        res.send({ code: 200, msg: "订单查询成功", data: result })
      }
    })
  }

  // 1.2 没传用户ID  按照订单类型查询,        后台管理员用**********
  else if (!uid) {
    const sql = `select * from pet_po where po_status="${status}"`;
    pool.query(sql, [status], (err, result) => {
      if (err) {
        next(err)
        return
      }
      // console.log(result);
      if (result.length === 0) {
        res.send({ code: 201, msg: "没有订单" })
      } else {
        res.send({ code: 200, msg: "订单查询成功", data: result })
      }
    })
  }
  //1.3 如果没传status 过来, 查某个用户(uid)的所有订单
  else if (!status) {
    const sql = `select * from pet_po where u_id=${uid} ORDER BY po_id desc limit ${pageNum},${pageSize}`;
    pool.query(sql, [uid, pageNum, pageSize], (err, result) => {
      if (err) {
        next(err)
        return
      }
      // console.log(result);
      if (result.length === 0) {
        res.send({ code: 201, msg: "没有订单" })
      } else {
        res.send({ code: 200, msg: "订单查询成功", data: result })
      }
    })
  }
  //1.4 传了 状态 status 过来, 查某个用户(uid)的 对应的status状态的订单
  else {
    const sql = `select * from pet_po where u_id=${uid} && po_status=${status} ORDER BY po_id desc limit ${pageNum},${pageSize}`;
    pool.query(sql, [uid, status, pageNum, pageSize], (err, result) => {
      if (err) {
        next(err)
        return
      }
      // console.log(result);
      if (result.length === 0) {
        res.send({ code: 201, msg: "没有订单" })
      } else {
        res.send({ code: 200, msg: "订单查询成功", data: result })
      }
    })
  }
});

// 2.用户生成订单

order.post('/add', (req, res, next) => {
  let obj = req.body;
  let u_id = obj.u_id;
  let pet_id = obj.pet_id;
  let bus_id = obj.bus_id;
  let po_price = obj.po_price;
  let po_petName = obj.po_petName;
  let po_userName = obj.po_userName;
  let po_time = obj.po_time;
  let po_userPhone = obj.po_userPhone;
  let po_beizhu = obj.po_beizhu;
  let po_status = obj.po_status;
  let po_linkman = obj.po_linkman;
  let po_bus_name = obj.po_bus_name;
  let po_number = obj.po_number;

  const sql = `insert into pet_po values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`;
  pool.query(
    sql,
    [
      ,
      u_id,
      pet_id,
      bus_id,
      po_price,
      po_petName,
      po_userName,
      po_time,
      po_userPhone,
      po_beizhu,
      po_status,
      po_linkman,
      po_bus_name,
      po_number,
    ],
    (err, data) => {
      if (err) {
        next(err);
        return;
      }
      if (data.affectedRows == 1) {
        res.send({ code: '200', msg: '添加订单成功!' });
      } else {
        res.send({ code: '201', msg: '添加失败!' });
        return;
      }
    }
  );
});

//3. 更改订单状态为 `已完成`
order.put('/changeDone', (req, res, next) => {
  let po_id = req.body.po_id;
  let sql = `UPDATE pet_po SET po_status=2 WHERE po_id=${po_id}`;
  pool.query(sql, [po_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '修改订单状态成功!' });
    } else {
      res.send({ code: '201', msg: '修改失败!' });
      return;
    }
  });
});

//4. 更改订单状态为 `已取消`
order.put('/changeCancel', (req, res, next) => {
  let po_id = req.body.po_id;
  let sql = `UPDATE pet_po SET po_status=3 WHERE po_id=${po_id}`;
  pool.query(sql, [po_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '修改订单状态成功!' });
    } else {
      res.send({ code: '201', msg: '修改失败!' });
      return;
    }
  });
});

//5 删除订单
order.post('/delete', (req, res, next) => {
  let po_id = req.body.po_id;
  let sql = `DELETE FROM pet_po WHERE po_id=${po_id}`;
  pool.query(sql, [po_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '删除订单成功!' });
    } else {
      res.send({ code: '201', msg: '删除失败!' });
      return;
    }
  });
});
//导出
module.exports = order
